﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace PreProcessing
{
    class Normalizer
    {
        //Dictionary<string, int> sens10 = new Dictionary<string, int>();
        //Dictionary<string, int> sens5 = new Dictionary<string, int>();
        //Dictionary<string, int> sens4 = new Dictionary<string, int>();
        //Dictionary<string, int> sens32 = new Dictionary<string, int>();
        //Dictionary<string, int> sens1 = new Dictionary<string, int>();
        Dictionary<string, int> sens = new Dictionary<string, int>();
        
        public void process(string input)
        {
            //input = Regex.Replace(input, Pattern.code, "");
            input = Regex.Replace(input, Pattern.codeDT, ".");
            input = Regex.Replace(input, Pattern.code, ".");
            input = Regex.Replace(input, Pattern.tag, ".");
            input = Regex.Replace(input, Pattern.line, ".");
            input = Regex.Replace(input, @"\d+|\*", ".");
            input=Regex.Replace(input,Pattern.space, " ");
            foreach (string asen in Regex.Split(input, Pattern.sign))
            {
                string sen = asen.Trim();
                if (Regex.IsMatch(sen, @"\w+"))
                {
                    if (sens.ContainsKey(sen))
                    {
                        sens[sen] += 1;
                    }
                    else
                    {
                        sens.Add(sen, 1);
                    }
                }
            }

        }

        public void readfile(string path)
        {
            string[] files = Directory.GetFiles(path, "*.txt", SearchOption.AllDirectories);
            foreach (string file in files)
            {
                string input = File.ReadAllText(file);
                process(input.ToLower());
            }
        }

        public void writetofile(string path)
        {
            StreamWriter sw = new StreamWriter(path);
            foreach (KeyValuePair<string,int> pair in sens)
            {
                sw.WriteLine(pair.Key + ":" + pair.Value);
            }
            sw.Flush();
            sw.Close();
        }
    }
}
